<template>
  <div class="edit">
    <a-modal v-model:visible="visible" title="编辑资产配置" @ok="onOk">
      <a-form :label-col="{ span: 6 }" :wrapper-col="{ span: 16 }">
        <a-form-item label="接口名称" v-bind="validateInfos.catalogInterName">
          <a-input v-model:value="form.state.catalogInterName" placeholder="请输入" :maxlength="20" />
        </a-form-item>
        <a-form-item label="接口URI" v-bind="validateInfos.catalogInterUrl">
          <a-input v-model:value="form.state.catalogInterUrl" placeholder="请输入" />
        </a-form-item>
        <a-form-item label="扩展信息" v-bind="validateInfos.extendedInfo">
          <a-textarea v-model:value="form.state.extendedInfo" placeholder="请输入" />
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>

<script lang="ts" setup>
  import { Form, message } from 'ant-design-vue'
  import { computed, reactive, ref, watch } from 'vue'

  import { requestEditCatalogInterface } from '/@/api/business/space'
  import { requestEditService, requestGetServiceByUuid } from '/@/api/manage/openApiService'
  import { HttpCodeEnum } from '/@/enums/httpEnum'

  const props = defineProps({
    visible: { type: Boolean, default: false },
    data: { type: Object, default: () => {} },
  })

  const emits = defineEmits(['update:visible', 'success'])
  const visible = computed({
    get: () => props.visible,
    set: (val: boolean) => emits('update:visible', val),
  })

  watch(
    () => visible.value,
    (newVal, oldVal) => {
      if (newVal && !oldVal) {
        initData()
        Object.assign(form.state, props.data)
      }
    },
  )
  const initData = () => {
    resetFields()
  }

  const onImageChange = (e) => {
    const file = e.target.files[0]
    if (file) {
      const reader = new FileReader()
      reader.onload = (event: any) => {
        // 读取完成后，event.target.result就是图片的Base64编码
        const base64Image = event.target.result
        form.state.logo = base64Image
        console.log(base64Image) // 输出Base64编码的图片数据
      }
      // 读取文件内容
      reader.readAsDataURL(file)
    } else {
      console.log('No file selected')
    }
  }

  /** 表单配置 */
  const form = reactive({
    state: {},
    rule: {
      catalogInterName: [{ required: true, message: '请输入接口名称' }],
      catalogInterUrl: [{ required: true, message: '请输入接口地址' }],
    },
    searchSuccess: false,
    onUserPhoneSearch: () => validate('userPhone'),
  })
  /** 表单hook */
  const { validate, validateInfos, resetFields } = Form.useForm(form.state, form.rule)

  /**
   * 确定
   */
  const onOk = () => {
    validate()
      .then(async () => {
        const { responseCode } = await requestEditCatalogInterface({ ...form.state })
        if (responseCode === HttpCodeEnum.SUCCESS) {
          message.success('编辑成功')
          visible.value = false
          emits('success')
        }
      })
      .catch(() => {})
  }
</script>

<style lang="less" scoped>
  .logo {
    position: absolute;
    top: 0;
    width: 100px;
    height: 100px;
    opacity: 0;
    cursor: pointer;
  }
</style>
